package com.jf.identity.util;

import com.jf.identity.exception.ServiceException;
import org.springframework.util.Base64Utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by x on 2018/4/11.
 * 此工具类包含2个方法
 * 方法一：postPicAtLocal()用于将用户上传的图片保存到本地，并返回路径名
 * 方法二：deleteLocalPic()用于删除对应路径的图片
 */
public class PostPictureUtil {
    public static String drive = PropertiesUtil.getProperty("image.drive");
    public static String read = PropertiesUtil.getProperty("image.read");
    //    private static String imagePathExaminer = drive + File.separator + "picture" + File.separator + "examiner";
//    private static String imagePathMessageReply = drive + File.separator + "picture" + File.separator + "messageReply";
//    private static String imagePathEmergencyReport = drive + File.separator + "picture" + File.separator + "emergencyReport";
//    private static String imagePathEmergencyRead = read + File.separator + "picture" + File.separator + "emergencyReport";
    private static String examinerPhotos = File.separator + "examiner_photos";
    private static String examinerFacePhotos = File.separator + "examiner_face_photos";
    private static String examineePhotos = File.separator + "examinee_photos";
    private static String examineeFacePhotos = File.separator + "examinee_face_photos";
    private static String examineeCardPhotos = File.separator + "examinee_card_photos";
    private static String examineeVodPhotos = File.separator + "examinee_vod_photos";
    private static String emergencyPhotos = File.separator + "emergency_photos";
    private static String placeMsgPhotos = File.separator + "place_msg_photos";

    public static String postPicAtLocal(String base64Data, String type , String examId) {
        String exUrl = drive + File.separator + examId;
        String dataPrix;
        String data;
        String[] d = base64Data.split("base64,");
        if (d.length == 2) {
            dataPrix = d[0];
            data = d[1];
        } else {
            throw new ServiceException("上传失败，数据不合法");
        }
        String suffix;
        String contentType;
        //编码的jpeg图片数据
        if ("data:image/jpeg;".equalsIgnoreCase(dataPrix)) {
            suffix = ".jpg";
//            contentType = "image/jpeg";
            //编码的icon图片数据
        } else if ("data:image/x-icon;".equalsIgnoreCase(dataPrix)) {
            suffix = ".ico";
//            contentType = "image/x-icon";
            //编码的gif图片数据
        } else if ("data:image/gif;".equalsIgnoreCase(dataPrix)) {
            suffix = ".gif";
//            contentType = "image/gif";
            //编码的png图片数据
        } else if ("data:image/png;".equalsIgnoreCase(dataPrix)) {
            suffix = ".png";
//            contentType = "image/png";
        } else if ("data:image/bmp;".equalsIgnoreCase(dataPrix)) {
            suffix = ".bmp";
//            contentType = "image/bmp";
        } else {
            throw new ServiceException("上传图片格式不合法");
        }
        String fileName = UUIDUtil.generateUUID() + suffix;
        byte[] bs = Base64Utils.decodeFromString(data);
        //判断图片的来源选择不同的路径存储
        String imagePath;
        switch (type) {
//            case "1":
//                imagePath = imagePathExaminer;
//                break;
//            case "2":
//                imagePath = imagePathMessageReply;
//                break;
//            case "3":
//                imagePath = imagePathEmergencyReport;
//                break;
//            case "4":
//                imagePath = imagePathEmergencyRead;
//                break;
            case "1":
                imagePath = exUrl + examinerPhotos;
                break;
            case "2":
                imagePath = exUrl + examinerFacePhotos;
                break;
            case "3":
                imagePath = exUrl + examineePhotos;
                break;
            case "4":
                imagePath = exUrl + examineeFacePhotos;
                break;
            case "5":
                imagePath = exUrl + examineeCardPhotos;
                break;
            case "6":
                imagePath = exUrl + emergencyPhotos;
                break;
            case "7":
                imagePath = exUrl + placeMsgPhotos;
                break;
            case "8":
                imagePath = exUrl + examineeVodPhotos;
                break;
            default:
                throw new ServiceException("图片路径非法");
        }
//        if ("1".equals(type))
//            imagePath = imagePathExaminer;
//        if("2".equals(type))
//            imagePath = imagePathMessageReply;
//        if("3".equals(type))
//            imagePath = imagePathEmergencyReport;
//        if(imagePath == null)
//            throw new ServiceException("图片路径非法");
        File imageFile = new File(imagePath + File.separator + fileName);
        if (!imageFile.getParentFile().exists()) {
            boolean made = imageFile.getParentFile().mkdirs();
            if (!made) {
                throw new ServiceException("图片路径异常");
            }
        }
        try (FileOutputStream os = new FileOutputStream(imageFile)) {
            os.write(bs);
            os.flush();
        } catch (IOException e) {
            throw new ServiceException("图片存储失败", e);
        }
        String out = imageFile.toString().substring(2);
        return out;
    }

    public static boolean deleteLocalPic(String imageUrl) {
        File imageFile = new File(imageUrl);
        return imageFile.exists() && imageFile.isFile() && imageFile.delete();
    }
}
